package com.moon.game.command.guest.lair;

import java.util.List;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.service.LairService;
import com.moon.game.service.PlayerService;
import com.moon.game.util.GDateUtils;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.Tower;

/**
 * @description 出租我的孵化塔
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-25 上午11:05:31
 */
public class RentTheLairCmd extends BaseCmd {

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		PlayerVo player = req.getPlayer();
		List<Tower> towers = player.getTowerList();
		if (towers == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "检测不到你的孵化塔，不能出租！"));
			return;
		}
		Integer towerId = Integer.parseInt(ps[0]);
		if (towerId == null) {
			doResponsePsError(req);
			return;
		}
		Tower theTower = LairService.findLair(towers, towerId);
		//没有指定孵化塔
		if (theTower == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "你当前没有指定的孵化塔！"));
			return;
		}
		req.setVO("tower_id", towerId);
		if (theTower.getCurrEndure() < Constants.RENT_LAIR_MIN_ENDURE) {
			//持久太低
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_LAIR_ENDURE, "你要出租的孵化塔持久太低了，请先修复再出租！"));
			return;
		}
		
		int leftRefreashTime = 0;
		switch(theTower.getHierarchy()){
		case 0:
			leftRefreashTime = theTower.getAnimalCalledTime() + Constants.COMMON_FREASH_TIME - GDateUtils.now();
			break;
		case 1:
			leftRefreashTime = theTower.getAnimalCalledTime() + Constants.IMMORTAL_FREASH_TIME - GDateUtils.now();
			break;
		case 2:
			leftRefreashTime = theTower.getAnimalCalledTime() + Constants.GOD_FREASH_TIME - GDateUtils.now();
			break;
		}
		
		if (leftRefreashTime <= Constants.PSYCHEDELIC_COMMON_ANIMAL_TIME) {
			//珍兽驻足时间不够
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_RENT_ANIMAL_LEFT_TIME, "要出租的孵化塔塔内珍兽剩余驻足时间不足" + GDateUtils.formatSeconds(Constants.PSYCHEDELIC_COMMON_ANIMAL_TIME) + "，不能出租！"));
			return;
		}
		
		if (theTower.getIncubationTimes() < Constants.RENT_LAIR_MIN_INCU_TIMES) {
			//成功孵化次数太少
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_RENT_TIMES, "要出租的孵化塔没有经历孵化考验(至少孵化成功" + Constants.RENT_LAIR_MIN_INCU_TIMES+ "次)！"));
			return;
		}
		
		if (ps.length < 2) {
			//没有标价表示标价前界面
			req.setVO("input_price", true);
			response(req, req.getClientResponse());
			return;
		}
		Integer incubationPrice = Integer.parseInt(ps[1]);
		if (incubationPrice == null) {
			doResponsePsError(req);
			return;
		}
		
		if (incubationPrice < 0 || incubationPrice >= 1000) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_SYS, "请输入0~999之间的纯数字！"));
			return;
		}
		
		//出租我的孵化塔
		int ret = LairService.rentingLair(theTower, player, incubationPrice);
		String rentInfo = "";
		if (ret > 0) {
			rentInfo = "孵化塔租赁处出租异常火爆导致名额已满，最快到期的孵化塔是" + GDateUtils.formatSeconds(ret) + "，你可以到时候来看看，不过他是否续期，我也说不准！";
		}else if (ret == -2) {
			rentInfo = "当前孵化塔已经在出租了，不能重复出租！";
		}else if (ret == -1){
			rentInfo = "出租成功！";
			PlayerService.doUpdatePlayer(req.getSid(), req.getPlayerId(), player);
		}
		req.setVO("rent_info", rentInfo);
		
		response(req, req.getClientResponse());
	}

}
